package com.ruoyi.file.controller;

import com.ruoyi.file.service.MinioSysFileServiceImpl;
import com.ruoyi.file.utils.FileUploadUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.file.FileUtils;
import com.ruoyi.file.service.ISysFileService;
import com.ruoyi.system.api.domain.SysFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import static com.ruoyi.file.utils.FileUploadUtils.determineContentType;

/**
 * 文件请求处理
 * 
 * @author ruoyi
 */
@RestController
public class SysFileController
{
    private static final Logger log = LoggerFactory.getLogger(SysFileController.class);

    @Autowired
    private MinioSysFileServiceImpl sysFileService;

    /**
     * 文件上传请求
     */
    @PostMapping("upload")
    public R<SysFile> upload(MultipartFile file)
    {
        try
        {
            // 计算文件的MD5值
            String md5 = FileUploadUtils.calculateMD5(file.getInputStream());
            log.info("File MD5: {}", md5);

            // 上传并返回访问地址
            String url = sysFileService.uploadFile(file);
            SysFile sysFile = new SysFile();
            sysFile.setName(FileUtils.getName(url));
            sysFile.setUrl(url);
            sysFile.setMd5(md5); // 假设SysFile类中有md5字段
            return R.ok(sysFile);
        }
        catch (Exception e)
        {
            log.error("上传文件失败", e);
            return R.fail(e.getMessage());
        }
    }


    /**
     * 通过文件地址获取文件
     *
     * @param fileUrl 文件的访问地址
     * @return 文件的输入流
     */
    @GetMapping(value = "getFile")
    public String getFileMd5(@RequestParam("fileUrl") String fileUrl) {
        try {
            InputStream inputStream = sysFileService.getFile(fileUrl);
            if (inputStream == null) {
                return null;
            }
            String md5 = FileUploadUtils.calculateMD5(inputStream);
            return md5;
        } catch (Exception e) {
            log.error("获取文件失败", e);
            return null;
        }
    }

/*    public class CustomInputStreamResource extends InputStreamResource {
        private final byte[] content;

        public CustomInputStreamResource(InputStream inputStream) throws IOException {
            super(inputStream);
            // 在构造函数中将输入流的内容复制到字节数组中
            this.content = StreamUtils.copyToByteArray(inputStream);
        }

        @Override
        public InputStream getInputStream() throws IOException {
            // 每次返回一个新的 ByteArrayInputStream
            return new ByteArrayInputStream(content);
        }
    }*/
}
